#include <pybind11/embed.h>
#include <pybind11/stl.h>
#include <iostream>
#include <filesystem>

#include <QApplication>
#include "MainWindow.h"

namespace py = pybind11;
namespace fs = std::filesystem;

int test1();

int test3();

int main(int argc, char *argv[]) {
    std::system("chcp 65001");

    QApplication app(argc, argv);
    MainWindow mainWindow;
    mainWindow.show();

    return app.exec();
}

int test1()
{
    try {
        // 1. 初始化解释器
        py::scoped_interpreter guard{};

        // 2. 设置 Python 路径
        auto sys = py::module_::import("sys");
        sys.attr("path").attr("append")("E:/code/CPP/pycpp/out/build/x64-debug/bin/Debug/python");
        std::cout << "================\n";
        sys.attr("path").attr("append")("E:/code/CPP/ocean1st-visualize/python");
        std::cout << "----------------\n";
#if 1
        //auto sys = py::module_::import("sys");
        //fs::path script_path = fs::current_path() / "python";
        //sys.attr("path").attr("append")(script_path.string());
        //sys.attr("path").attr("append")("E:/code/CPP/pycpp/out/build/x64-debug/bin/Debug/python");
#else
        py::exec(R"(
            import sys
            sys.path.append('E:/code/CPP/pycpp/out/build/x64-debug/bin/Debug/python')  # 添加 Python 脚本目录到 sys.path
        )");
#endif   
        // 3. 导入自定义模块
        auto my_module = py::module_::import("my_script");

        // 4. 调用 Python 函数
        auto result = my_module.attr("calculate")(10, 20).cast<int>();
        std::cout << "Python calculation result: " << result << std::endl;
    }
    catch (const py::error_already_set& e) {
        e.trace();
        std::cerr << "Python error: " << e.what() << std::endl;
        return 0;
    }
    catch (const std::exception& e) {
        std::cerr << "C++ error: " << e.what() << std::endl;
        return 0;
    }

    return 0;
}


int test3()
{
    try {
        py::scoped_interpreter guard{};

        auto sys = py::module_::import("sys");
        sys.attr("path").attr("append")("E:/code/CPP/ocean1st-visualize/python");
        std::cout << "----------------\n";

        std::vector<std::string> files = {
        R"(D:\data\fio\chanpin\res\DH\20250205\DH_2025020512_UVEL.NC)",
        R"(D:\data\fio\chanpin\res\DH\20250205\DH_2025020512_VVEL.NC)"
        };
        std::vector<std::string> vars = { "uvel", "vvel" };
        std::pair<double, double> point1 = { 120.0, 30.0 };
        std::pair<double, double> point2 = { 125.0, 32.0 };

        py::list files_list = py::cast(files);
        py::list vars_list = py::cast(vars);
        auto my_module = py::module_::import("plot_cross_section");
        my_module.attr("plot_cross_section")(
            py::cast(files), 
            py::cast(vars),
            py::str("lon"), py::str("lat"), py::str("lay"), py::str("time"),
            py::make_tuple(point1.first, point1.second),
            py::make_tuple(point2.first, point2.second),
            py::int_(0),
            py::float_(0.0), py::float_(100.0), py::int_(10), py::float_(1.0), py::bool_(true)
        );
    } catch (const py::error_already_set& e) {
        std::cerr << "Python error: " << e.what() << std::endl;
        return false;
    } catch (const std::exception& e) {
        std::cerr << "C++ error: " << e.what() << std::endl;
        return false;
    }
    return 0;
}